﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.DAL;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Data;

namespace IHISCETeim.BLL
{
    public class OInspectionController : BaseController
    {
        /// <summary>
        /// 返回-1 表示机号已存在
        /// 返回-2 表示仪表号码已存在
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="NGItems"></param>
        /// <returns></returns>
        public static int Add(OInspectionInfo obj,List<ONGInspectionInfo> NGItems)
        {
            return OInspectionDAL.Add(obj,NGItems);
        }       
        public static bool Delete(int ID)
        {
            return OInspectionDAL.Delete(ID);
        }
        public static OInspectionInfo GetList(int ID)
        {
            return CBO.FillObject<OInspectionInfo>(OInspectionDAL.GetList(ID));
        }
        /// <summary>
        /// 获取完检单或完成检查书对应的NG项
        /// </summary>
        /// <param name="SerialNumber"></param>
        /// <param name="SourceType">完检单 / 完成检查书</param>
        /// <returns></returns>
        public static DataTable GetNGItemList(int FromId, string SourceType)
        {
            DataTable DT = OInspectionDAL.GetNGItemList(FromId, SourceType);
            //if(SourceType=="完成检查书")
            //{
            //    if(DT.Rows.Count<=0)
            //    {
            //        DataRow dr = DT.NewRow();
            //        DT.Rows.Add(dr);
            //        return DT;
            //    }
            //    else
            //        return DT;
            //}
            return DT;
        }
        public static DataTable GetPagination(string SourceType,string MachineType, string SerialNumber, string DownlineDateFrom, string DownlineDateTo, string DJNGItemCode, string BZNGItemCode, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            return OInspectionDAL.GetPagination(SourceType,MachineType, SerialNumber, DownlineDateFrom, DownlineDateTo, DJNGItemCode, BZNGItemCode, PageIndex, PageSize, Sort, ref  TotalCount);
        }

        public static OTallyprojectInfo OTallyproject_GetByItemCode(string ItemCode)
        {
            OTallyprojectInfo obj=  CBO.FillObject<OTallyprojectInfo>(OInspectionDAL.OTallyproject_GetByItemCode(ItemCode));
            if (obj == null)
                obj = new OTallyprojectInfo();
            return obj;
        }


        public static DataTable OTallyproject_GetALL(int itemType)
        {
            DataTable dt = DC.Get<DataTable>(GetDataCacheKey(OTallyprojectGetALLByItemType,itemType));
            if (dt == null)
            {
                dt = OInspectionDAL.OTallyproject_GetALL(itemType);
                DC.Add(GetDataCacheKey(OTallyprojectGetALLByItemType, itemType), dt);
            }
            return dt;
        }
        /// <summary>
        /// 检查项目查询
        /// </summary>
        /// <param name="key"></param>
        /// <param name="toprow"></param>
        /// <returns></returns>
        public static DataTable OTallyproject_GetALL(int itemType,string key, int toprow)
        {
            DataTable dt = OTallyproject_GetALL(itemType);
            DataRow[] drs;
            if (string.IsNullOrEmpty(key))
                drs = dt.Select("");
            else
                drs = dt.Select("ItemCode like '%" + key + "%' or ItemName like '%" + key + "%'");
            if (drs.Count() <= 0)
                return null;
            dt = drs.Take(toprow).CopyToDataTable();
            return dt;
        }

        public static DataTable OIssueCode_GetALL()
        {
            DataTable dt = DC.Get<DataTable>(OIssueCodeGetALL);
            if (dt == null)
            {
                dt = OInspectionDAL.OIssueCode_GetALL();
                DC.Add(OIssueCodeGetALL, dt);
            }
            return dt;
        }
        /// <summary>
        /// 查询问题代码
        /// </summary>
        /// <param name="key"></param>
        /// <param name="toprow"></param>
        /// <returns></returns>
        public static DataTable OIssueCode_GetALL(string key, int toprow)
        {
            DataTable dt = OIssueCode_GetALL();
            DataRow[] drs;
            if (string.IsNullOrEmpty(key))
                drs = dt.Select("");
            else
                drs = dt.Select("CodeTag like '%" + key + "%' or CodeName like '%" + key + "%'");
            if (drs.Count() <= 0)
                return null;
            dt = drs.Take(toprow).CopyToDataTable();
            return dt;
        }

        /// <summary>
        /// 获取导出数据
        /// </summary>
        /// <param name="MachineType"></param>
        /// <param name="SerialNumber"></param>
        /// <param name="DownlineDateFrom"></param>
        /// <param name="DownlineDateTo"></param>
        /// <param name="DJNGItemCode"></param>
        /// <returns></returns>
        public static DataTable GetExeclDate(string MachineType, string SerialNumber, string DownlineDateFrom, string DownlineDateTo, string DJNGItemCode)
        {
           return OInspectionDAL.GetExeclDate(MachineType, SerialNumber, DownlineDateFrom, DownlineDateTo, DJNGItemCode);
        }
        /// <summary>
        /// 获取所有的NG项说明 
        /// </summary>
        /// <returns></returns>
        private static DataTable GetNGExceptionrecordList()
        {
            DataTable dt = DC.Get<DataTable>(GetNGExceptionrecordListKey);
            if (dt == null)
            { 
                dt=OInspectionDAL.GetNGExceptionrecordList();
                DC.Add(GetNGExceptionrecordListKey, dt);
            }
            return dt;
        }
        /// <summary>
        /// 获取自动完成要显示的NG项
        /// </summary>
        /// <param name="key"></param>
        /// <param name="toprow"></param>
        /// <returns></returns>
        public static DataTable GetNGExceptionrecordList(string key, int toprow)
        {

            DataTable dt = GetNGExceptionrecordList();
            DataRow[] drs;
            if (string.IsNullOrEmpty(key))
                drs = dt.Select("");
            else
                drs = dt.Select(" Exceptionrecord like '%" + key + "%'");
            if (drs.Count() <= 0)
                return null;
            dt = drs.Take(toprow).CopyToDataTable();
            return dt;

        }

        #region 报表相关
        /// <summary>
        /// 1.1一次性能检测合格率 31 台, 性能检测总台数:47 台 .一次性能检测合格率：31/47X100%≈66﹪（一次性能检测合格数/性能检测台数X100%）。
        /// </summary>
        /// <param name="monthstart"></param>
        /// <param name="monthend"></param>
        /// <returns></returns>
        public static DataTable OInspection_GetReport1(DateTime monthstart, DateTime monthend)
        {
            return OInspectionDAL.OInspection_GetReport1(monthstart, monthend);
        }
        /// <summary>
        /// 1.2 一次出入库检查合格台数：55台，出入库台数：108台 一次出入库合格率：55/108X100%≈50.93﹪（一次出入库合格数/出入库台数X100%）。
        /// </summary>
        /// <param name="monthstart"></param>
        /// <param name="monthend"></param>
        /// <returns></returns>
        public static DataTable OInspection_GetReport2(DateTime monthstart, DateTime monthend)
        {
            return OInspectionDAL.OInspection_GetReport2(monthstart, monthend);
        }

        /// <summary>
        /// 获取指定时间段内机型
        /// </summary>
        /// <param name="monthstart"></param>
        /// <param name="monthend"></param>
        /// <returns></returns>
        public static DataTable OInspection_GetReportMacType3(DateTime monthstart, DateTime monthend)
        {
            return OInspectionDAL.OInspection_GetReportMacType3(monthstart, monthend);
        }
        /// <summary>
        /// 二.各机型不良率
        /// </summary>
        /// <param name="mactype">机型</param>
        /// <param name="ItemType">要获取的项目</param>
        /// <param name="monthstart"></param>
        /// <param name="monthend"></param>
        /// <returns></returns>
        public static object OInspection_GetReport3(string mactype, string ItemType, DateTime monthstart, DateTime monthend)
        {
            return OInspectionDAL.OInspection_GetReport3(mactype, ItemType,monthstart, monthend);
        }

          /// <summary>
        /// 三.生产品质信息表
        /// </summary>
        /// <param name="monthstart"></param>
        /// <param name="monthend"></param>
        /// <returns></returns>
        public static DataTable OInspection_GetReport4(DateTime monthstart, DateTime monthend)
        {
            return OInspectionDAL.OInspection_GetReport4(monthstart, monthend);
        }
        /// <summary>
        /// 四.重点问题汇总
        /// </summary>
        /// <param name="monthstart"></param>
        /// <param name="monthend"></param>
        /// <returns></returns>
        public static DataTable OInspection_GetReport5(DateTime monthstart, DateTime monthend)
        {
            return OInspectionDAL.OInspection_GetReport5(monthstart, monthend);
        }
        /// <summary>
        /// 五.外协件（性能测试与出入库检查）品异汇总
        /// </summary>
        /// <param name="monthstart"></param>
        /// <param name="monthend"></param>
        /// <returns></returns>
        public static DataTable OInspection_GetReport6(DateTime monthstart, DateTime monthend)
        {
            return OInspectionDAL.OInspection_GetReport6(monthstart, monthend);
        }
        #endregion

        public static DataTable OInspection_ExportNGListByDate(string MachineType, string SerialNumber,string FormDate, string ToDate)
        {
            return OInspectionDAL.OInspection_ExportNGListByDate(MachineType, SerialNumber, FormDate, ToDate);
        }
    }
}
